package com.yibing.test.enumtest;

import java.util.Map;
import java.util.EnumMap;

public enum Phase {

	SOLID, LIQUID, GAS;
	
	public enum Transition {
		MELT(SOLID, LIQUID), FREEZE(LIQUID, SOLID),
		BOIL(LIQUID, GAS),   CONDENSE(GAS, LIQUID),
		SUBLIME(SOLID, GAS), DEPOSIT(GAS, SOLID);
		
		private final Phase src;
		private final Phase dst;
		
		Transition(Phase src, Phase dst) {
			this.src = src;
			this.dst = dst;
		}
		
		/**
		 * Use a EnumMap<?, EnumMap<?, ?>> to emulate a 2-dimension 
		 * data structure. In this case, it's a pair of Phases -> Transition
		 */
		private static final Map<Phase, Map<Phase, Transition>> m = 
			new EnumMap<Phase, Map<Phase, Transition>>(Phase.class);
		
		static {
			for (Phase p : Phase.values()) {
				m.put(p, new EnumMap<Phase, Transition>(Phase.class));
			}
			
			for (Transition t : Transition.values()) {
				m.get(t.src).put(t.dst, t);
			}
		}
		
		public static final Transition getTransition(Phase src, Phase dst) {
			return m.get(src).get(dst);
		}
		
	}
	
}
